﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BLL
{
    public class BaseBLL
    {
        System.Data.SqlClient.SqlConnection _Connection;
        System.Data.SqlClient.SqlTransaction _Transaction;

        public BaseBLL()
        {
            _Connection = null;
            _Transaction = null;
        }

        protected System.Data.SqlClient.SqlConnection OpenConnection()
        {
            if (_Connection == null)
                _Connection = new System.Data.SqlClient.SqlConnection(GetConnectionString());
            if (_Connection.State != System.Data.ConnectionState.Open)
                _Connection.Open();
            return _Connection;
        }

        protected System.Data.SqlClient.SqlTransaction BeginTransaction()
        {
            if (_Connection == null)
                OpenConnection();
            if (_Connection.State != System.Data.ConnectionState.Open)
                _Connection.Open();
            _Transaction = _Connection.BeginTransaction();
            return _Transaction;
        }

        protected void Commit()
        {
            _Transaction.Commit();
        }

        protected void Rollback()
        {
            _Transaction.Rollback();
        }

        protected void CloseConnection()
        {
            _Connection.Close();
        }

        private String GetConnectionString()
        {
            return DAL.sqlHelper.GetConnectionString();
        }

        public static bool TestApplicationConf()
        {
            return DAL.sqlHelper.TestConnection();
        }
    }
}
